<?php

declare(strict_types=1);

namespace App\Chat\Controller;

use App\Chat\Annotation\ChatAuth;
use App\Chat\Request\TalkKeywordCateRequest;
use App\CustHub\Service\ChTalkKeywordCateService;
use App\CustHub\Service\WordGroupService;
use Hyperf\Context\Context;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\PostMapping;

#[Controller(prefix: 'ch/chat/keyword/cate'),ChatAuth]
class KeywordCateController extends ChatBaseController
{
    #[Inject]
    protected ChTalkKeywordCateService $service;

    #[Inject]
    protected WordGroupService $wordGroupService;

    /**
     * 列表
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('list')]
    public function list(): \Psr\Http\Message\ResponseInterface
    {
        $data = $this->request->all();
        $data['select'] = 'id,name';
        $data['main_uid'] = Context::get('main_uid');
        return $this->success($this->service->getPageList($data,false));
    }

    /**
     * 新增
     * @param TalkKeywordCateRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('save')]
    public function save(TalkKeywordCateRequest $request): \Psr\Http\Message\ResponseInterface
    {
        $data = $request->all();
        $uid = $data['uid'];
        $type = $data['type'];
        $groupData['main_uid'] = Context::get('main_uid');
        $groupData['uid'] = Context::get('m_uid');
        $groupData['title'] = $data['name'];
        $groupData['p_id'] = $data['pid'];
        if($type == 0){
            $groupData['cs_id'] = 0;
        }else{
            $groupData['cs_id'] = $uid;
        }
        $groupData['is_personal'] = $type;
        return $this->wordGroupService->save($groupData) ? $this->success() : $this->error();
    }

    #[PostMapping('update')]
    public function update(TalkKeywordCateRequest $request): \Psr\Http\Message\ResponseInterface
    {
        $data = $request->all();
        $groupData['title'] = $data['name'];
        $groupData['p_id'] = $data['pid'];
        return $this->wordGroupService->update($request->input('id'),$groupData) ? $this->success() : $this->error();
    }

    /**
     * 删除
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('delete')]
    public function delete(TalkKeywordCateRequest $request): \Psr\Http\Message\ResponseInterface
    {
        return $this->wordGroupService->delGroup($request->all()) ? $this->success() : $this->error();
    }
}